﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Domain.Enums;
using Domain.UnitOfWork;
using Mestedur.Models.Membership;
using Services;

namespace Mestedur.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/
        private readonly IMembershipService membershipService;
        private IFormAuthenticationService formsService;
        private IUnitOfWorkFactory unitOfWorkFactory;
        private IUserService userService;
        public AccountController(IMembershipService membershipService, IFormAuthenticationService formsService, IUnitOfWorkFactory unitOfWorkFactory, IUserService userService)
        {
            this.membershipService = membershipService;
            this.userService = userService;
            this.formsService = formsService;
            this.unitOfWorkFactory = unitOfWorkFactory;
        }


        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            using (unitOfWorkFactory.Create())
            {
                if (ModelState.IsValid)
                {
                    if (membershipService.ValidateUser(model.UserName, model.Password))
                    {
                        formsService.SignIn(model.UserName, model.RememberMe);
                        var user = userService.GetUserByName(model.UserName);
                        if (user.UserName == "Administrator") user.Role = UserRoles.Administrator;
                        membershipService.RegisterRole(user);
                        if (Url.IsLocalUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "MainPage");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    }
                }
                return View(model);
            }
        }

        public ActionResult LogOff()
        {
            formsService.SignOut();

            return RedirectToAction("Index", "MainPage");
        }

        // **************************************
        // URL: /Account/Register
        // **************************************

        public ActionResult Register()
        {
            ViewBag.PasswordLength = membershipService.MinPasswordLength;
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            using (var unit = unitOfWorkFactory.Create())
            {
                if (ModelState.IsValid)
                {
                    // Attempt to register the user
                    MembershipCreateStatus createStatus = membershipService.CreateUser(model.UserName, model.Password,
                                                                                       model.Email);
                    

                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        formsService.SignIn(model.UserName, false /* createPersistentCookie */);
                        unit.Commit();
                        return RedirectToAction("Index", "MainPage");
                    }
                }

                // If we got this far, something failed, redisplay form
                ViewBag.PasswordLength = membershipService.MinPasswordLength;
                return View(model);
            }
        }
    }
}
